﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telecom.Data;

namespace Telecom.Service
{
    public class cardSer
    {
        private readonly EntitiesTelecom dbContext = new EntitiesTelecom();

        public cardSer()
        {
            dbContext.Configuration.LazyLoadingEnabled = false;
            dbContext.Configuration.ProxyCreationEnabled = false;
        }

        public int AddUpDate(CARD entity)
        {
            try
            {
                if (entity.ID == 0) dbContext.CARD.Add(entity);
                else dbContext.Entry(entity).State = System.Data.EntityState.Modified;
                dbContext.SaveChanges();
                return entity.ID;
            }
            catch (Exception e) { return 0; }
        }

        public List<CARD> _getList(int IdTb)
        {
            try
            {
                dbContext.Configuration.LazyLoadingEnabled = false;
                return dbContext.CARD.Where(p => p.DMTHIETBI == IdTb).ToList();
            }
            catch (Exception e)
            {
                List<CARD> _listReturn = new List<CARD>();
                return _listReturn;
            }
        }

        public CARD GetById(int id)
        {
            try
            {
                return dbContext.CARD.Where(p => p.ID == id).First();
            }
            catch (Exception e)
            {
                CARD _CARD = new CARD();
                return _CARD;
            }
        }

        public bool DeleteCard(int CardId)
        {
            try
            {
                var entity = GetById(CardId);
                dbContext.CARD.Remove(entity);
                dbContext.SaveChanges();

                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
    }
}
